package org.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.catalina.User;
import org.demo.entity.*;
import org.demo.mapper.CommunityMapper;
import org.demo.mapper.MessageMapper;
import org.demo.mapper.UsersMapper;
import org.demo.service.ICommunityService;
import org.demo.service.IMessageService;
import org.demo.service.IOrderIdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private IOrderIdService orderIdService;
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public void add(Message message) {
        String id = orderIdService.getId("MESS");
        message.setId(id);

        LocalDate today = LocalDate.now();
//        Date parsedDate = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());
        message.setCreateTime(today);

        save(message);
    }

    @Override
    public List<MessageDto> list(Message message) {

        List<Message> messageList=messageMapper.selectAllMess(message);

        List<MessageDto> messageDtos = new ArrayList<>();
        for (int i = 0; i < messageList.size(); i++) {
            Message mess = messageList.get(i);

            Users user = usersMapper.selectById(mess.getUserId());
            MessageDto messageDto = BeanUtil.copyProperties(mess, MessageDto.class);

            messageDto.setUserPic(user.getUserPic());
            messageDto.setNickname(user.getNickname());

            messageDtos.add(messageDto);

        }

        return messageDtos;
    }

    @Override
    public void update(Message message) {
        messageMapper.updateById(message);
    }

    @Override
    public void delete(String id) {
        messageMapper.deleteById(id);
    }
}
